<?php
namespace App\Models\Item;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Facades\Request;
//use Illuminate\Support\Facades\Config;
class Item{

    private $_lang = 'en';
    private $_files = [
        'item.json',
        'equipment.json',
        'enchant.json',
        'drawing.json',
        'enchant_material.json',
        'material.json',
        'rune.json',
        'gift.json',
        'skin_card.json',
    ];

    private $_transe;
    private $_item_all = [];
    const PAGE_LENGTH = 1000;

    public function __construct()
    {
        $this->LoadDataFromJson();
    }
    //
    public function LoadDataFromJson()
    {
        foreach($this->_files as $file)
        {
            $rf = base_path().'/app/Models/Item/Json/'. $file;
            $content = file_get_contents($rf);
            
            $this->_item_all[] =  json_decode($content, true);
        }
        
        $rf = base_path().'/app/Models/Item/Language/'.$this->GetDefaultLocale().'/languageconfig.json';
        $content = file_get_contents($rf);
        $this->_transe = json_decode($content, true);
        //\Log::debug("transe", [$this->_transe, $rf]);
    }

    public function GetDefaultLocale()
    {
        $lang = \Config::get("app.locale");
        
        $lang = $lang ?: $this->_lang;
        return strtolower($lang);
    }

    // 处理单个 item
    public function ItemDeal($obj)
    {
        
        $new_obj["id"] = $obj["id"]?:$obj['prop_id'];
        $new_obj["name"] = $this->Transifer($obj["name"]) . '' . $new_obj['id'];
        if ($obj['term_time']) {
            $new_obj["name"] .='['.$obj['term_time'] . ']';
        }
        return $new_obj;
    }


    public function Transifer($key, $locale = 'en')
    {
        return $this->_transe[$key]['value'] ?: $key;
    }

    public function GetItemCount(){
        $count = 0;
        foreach ($this->_item_all as $k => $items) {
            $count += count($items);
        }
        return $count;
    }

    public function GetItemArray($offset = 0, $all = null)
    {
        $item_all = [];
        $selected_count = 0;
        $step = 0;
        foreach($this->_item_all as $k => $items)
        {
            foreach($items as $kk => $obj)
            {
                $step++;
                if (isset($obj['released']) && $obj['released'] == 0 ) {
                    continue;
                }
                // 从$offset 起始位置 开始查询 包含 $offset 的位置
                if ($step < $offset) {
                    continue;
                }
                // 查询够 数 返回
                if ($selected_count >= 1000  && !$all) {
                    return $item_all;
                }
                $selected_count ++;
                $obj["id"] = $kk;
                $item_all[$kk] = $this->ItemDeal($obj);
            }
            
        }
        //\Log::debug('item load json from file', [$item_all, 'GetItemArray']);
        return $item_all;
    }
}